/*
 * $Id: proc.c,v 1.1 2006-04-14 14:18:29 bacon Exp $
 */

#include <xp/sys/proc.h>

#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/wait.h>

xp_pid_t xp_forkproc (int opt)
{
	xp_pid_t pid;

	pid = fork ();

	if (opt & XP_FORKPROC_DETACH)
	{
		int status;

		if (pid < 0) return -1;

		if (pid == 0)
		{
			/* child */
			pid = fork();
			if (pid < 0) exit (1);
			if (pid > 0) exit (0);
			return 0;
		}

		/* pid > 0 => parent */
		if (waitpid(pid,&status,0) == -1) return -1;
		if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) pid = -1;
	}

	return pid;
}

/* TODO
xp_pid_t xp_waitproc ()
{
	return -1;
}
*/

int xp_killproc (xp_pid_t pid, int sig)
{
	return kill (pid, sig);	
}
